package com.ljf.authority.infra.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljf.authority.domain.repository.AuthorityRelButtonApiRepository;
import com.ljf.authority.dto.query.AuthorityRelButtonApiQuery;
import com.ljf.authority.infra.mapper.AuthorityRelButtonApiMapper;
import com.ljf.authority.model.AuthorityRelButtonApi;
import com.ljf.authority.vo.AuthorityRelButtonApiVO;
import com.ljf.common.domain.entity.KeyCountModel;
import com.ljf.common.exception.ServiceException;
import com.ljf.common.infra.repository.impl.BasisRepositoryImpl;
import com.ljf.common.utils.CollectionUtil;
import com.ljf.common.utils.StringUtil;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 按钮API关联  数据访问接口实现
 *
 * @author LJF
 * @since 2024-10-30
 */
@Component
public class AuthorityRelButtonApiRepositoryImpl extends BasisRepositoryImpl<AuthorityRelButtonApiMapper, AuthorityRelButtonApi, AuthorityRelButtonApiQuery, AuthorityRelButtonApiVO> implements AuthorityRelButtonApiRepository {

    @Override
    protected QueryWrapper<AuthorityRelButtonApi> fillWrapper(AuthorityRelButtonApiQuery query, boolean sort) throws ServiceException {
        QueryWrapper<AuthorityRelButtonApi> wrapper = super.fillWrapper(query, sort);
        if (CollectionUtil.isNotEmpty(query.getButtonCodeList())) {
            wrapper.in(AuthorityRelButtonApi.COL_BUTTON_CODE, query.getButtonCodeList());
        }
        if (StringUtil.isNotEmpty(query.getButtonCodeRightLike())) {
            wrapper.likeRight(AuthorityRelButtonApi.COL_BUTTON_CODE, query.getButtonCodeRightLike());
        }
        if (CollectionUtil.isNotEmpty(query.getApiCodeList())) {
            wrapper.in(AuthorityRelButtonApi.COL_API_CODE, query.getApiCodeList());
        }
        return wrapper;
    }

    /**
     * 分组统计API编码数量
     *
     * @param buttonCodeList
     * @return
     * @throws ServiceException
     */
    @Override
    public List<KeyCountModel> countApiCodeGroupByButtonCode(List<String> buttonCodeList) {
        return mapper.countApiCodeGroupByButtonCode(buttonCodeList);
    }

    /**
     * 分组统计前端数量
     *
     * @param apiCodeList
     * @return
     * @throws ServiceException
     */
    @Override
    public List<KeyCountModel> countButtonCodeGroupByApiCode(List<String> apiCodeList) throws ServiceException {
        return mapper.countButtonCodeGroupByApiCode(apiCodeList);
    }

}
